#! /usr/bin/perl

#	Script gotten from "http://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-sqlite3-to-mysql"
#	Some edits done by Lucas McDaniel
#	Not guarenteed to work for all sqlite3 db.  May require more editing to fully convert

while ($line = <>){
    if (($line !~  /BEGIN TRANSACTION/) && ($line !~ /COMMIT/) && ($line !~ /sqlite_sequence/) && ($line !~ /CREATE UNIQUE INDEX/) && ($line !~ /PRAGMA foreign_keys=OFF/)){

        if ($line =~ /CREATE TABLE \"([a-z_]*)\"(.*)/){
                $name = $1;
                $sub = $2;
                $sub =~ s/\"//g;
                $line = "DROP TABLE IF EXISTS $name;\nCREATE TABLE IF NOT EXISTS $name$sub\n";
        }
        elsif ($line =~ /INSERT INTO \"([a-z_]*)\"(.*)/){
                $line = "INSERT INTO $1$2\n";
                $line =~ s/\"/\\\"/g;
                $line =~ s/\"/\'/g;
        }
	elsif ($line =~ /TEXT/){
		$line =~ s/TEXT/VARCHAR(80)/g;
	}
	elsif ($line =~ /NUMBER/){
		$line =~ s/NUMBER/INTEGER/g;
	}
	elsif ($line =~ /CONSTRAINT/){
		$line2 =~ s/,//g;
		$line ="";
	}
	elsif ($line =~ /CREATE INDEX/){
		$line =~ s/\"//g;
	}
	elsif ($line =~ /TRIGGER/){
		while ($line !~ /END;/){
			$line = <>;
		}
		$line = "";
	}
	else{
                $line =~ s/\'\'/\\\'/g;
        }
        $line =~ s/([^\\'])\'t\'(.)/$1THIS_IS_TRUE$2/g;
        $line =~ s/THIS_IS_TRUE/1/g;
        $line =~ s/([^\\'])\'f\'(.)/$1THIS_IS_FALSE$2/g;
        $line =~ s/THIS_IS_FALSE/0/g;
        $line =~ s/AUTOINCREMENT/AUTO_INCREMENT/g;
	if (length($line2) > 0){
	        print $line2;
	}
	$line2 = $line;
    }
}
print $line2;
